<?php
/*
 * class  for foget password in login form to send a email for given email addrerss 
 * if it included in data base
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

class AjaxChangePasswordAction extends CAction {

   public function run() {

      if (Yii::app()->request->getIsAjaxRequest()) {

         $modelEmail = User::model()->findByAttributes(array('email' => $_POST['email']));
         if ($modelEmail) {
            $message = new YiiMailMessage;
            $message->view = "mailTemplate";
            $message->subject = 'Change Password';
            //create tokenid
            $token = User::model()->generateRandomString();
            $model = new fogetPassword();
            $model->tokenid = $token;
            $model->user_id = $modelEmail->id;
            if ($model->save())
               $url = Yii::app()->createAbsoluteUrl('/user/updatePassword/?id=' . $token);
            $params = array('mailMassage' => $modelEmail, 'link' => $url);
            $message->setBody($params, 'text/html');
            $message->addTo($_POST['email']);
            $message->from = Yii::app()->params['fromEmail'];
            Yii::app()->mail->send($message);

            echo 'valid';
            exit;
         }else {
            echo 'invalid';
            exit;
         }
      }
   }
}
